package com.insurance.service.impl;

import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.entity.AuthenticationEnterpriseEntity;
import com.insurance.entity.AuthenticationUserEnterpriseEntity;
import com.insurance.service.AuthenticationEnterpriseService;
import com.insurance.service.AuthenticationService;
import com.insurance.service.AuthenticationUserEnterpriseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Style
 * @version 1.0
 * @date 2021/2/27
 **/
@Slf4j
@Service
public class AuthenticationServiceImpl implements AuthenticationService {

    @Autowired
    private AuthenticationEnterpriseService authenticationEnterpriseService;

    @Autowired
    private AuthenticationUserEnterpriseService authenticationUserEnterpriseService;

    /**
     * 保存企业信息
     *
     * @param authenticationEnterpriseEntity 企业信息
     * @return
     * @throws Exception
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public boolean saveAuthInfo(AuthenticationEnterpriseEntity authenticationEnterpriseEntity) throws Exception{
        authenticationEnterpriseEntity.setDeleteFlag(String.valueOf(DeleteFlagEnum.UN_DELETE.getCode()));
        if(authenticationEnterpriseEntity == null){
            log.error("【保存企业信息失败, 企业对象为空】");
            return false;
        }
        boolean saveEnterpriseFlag = authenticationEnterpriseService.save(authenticationEnterpriseEntity);
        if(!saveEnterpriseFlag){
            throw new Exception("【保存企业信息失败】");
        }
        AuthenticationUserEnterpriseEntity authenticationUserEnterpriseEntity = new AuthenticationUserEnterpriseEntity();
        authenticationUserEnterpriseEntity.setUserId(authenticationEnterpriseEntity.getCreatePersonId());
        authenticationUserEnterpriseEntity.setEnterpriseId(authenticationEnterpriseEntity.getId());
        boolean saveUserEnterpriseFlag = authenticationUserEnterpriseService.save(authenticationUserEnterpriseEntity);
        if(!saveUserEnterpriseFlag){
            throw new Exception("【保存人员企业关系信息失败】");
        }
        return true;
    }
}
